package com.livepan.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.livepan.server.dao.CategoryDOMapper;
import com.livepan.server.dataobject.CategoryDO;
import com.livepan.server.dataobject.CategoryDOExample;
import com.livepan.server.error.BusinessException;
import com.livepan.server.error.EmBusinessError;
import com.livepan.server.service.model.CategoryModel;
import com.livepan.server.service.model.PageModel;
import com.livepan.server.utils.CopyUtil;
import com.livepan.server.utils.UUIDUtil;
import com.livepan.server.utils.ValidatorUtil;
import com.livepan.server.validator.ValidationResult;
import com.livepan.server.validator.ValidatorImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.nio.cs.CharsetMapping;


import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryService {
    @Autowired
    private CategoryDOMapper categoryDOMapper;

    @Autowired
    private ValidatorImpl validator;

    public void list(PageModel pageModel) {
        PageHelper.startPage(pageModel.getPage(), pageModel.getSize());
        CategoryDOExample categoryDOExample = new CategoryDOExample();
        List<CategoryDO> categoryDOList = categoryDOMapper.selectByExample(categoryDOExample);
        PageInfo<CategoryDO> pageInfo = new PageInfo<>(categoryDOList);
        pageModel.setTotal(pageInfo.getTotal());
        List<CategoryModel> categoryModelList = categoryDOList.stream().map(
                this::convertCategoryModelFromCategoryDO
        ).collect(Collectors.toList());
        pageModel.setList(categoryModelList);
    }

    public List<CategoryModel> all() {
        CategoryDOExample categoryDOExample = new CategoryDOExample();
        categoryDOExample.setOrderByClause("sort asc");
        List<CategoryDO> categoryDOList = categoryDOMapper.selectByExample(categoryDOExample);
        List<CategoryModel> categoryModelList = categoryDOList.stream().map(
                this::convertCategoryModelFromCategoryDO
        ).collect(Collectors.toList());
        return categoryModelList;
    }

    public void save(CategoryModel categoryModel) throws BusinessException {
        ValidationResult validateResult = validator.validate(categoryModel);
        if (validateResult.isHasError()) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, validateResult.getErrMsg());
        }
        CategoryDO categoryDO = CopyUtil.copy(categoryModel, CategoryDO.class);
        if (categoryDOMapper.selectByPrimaryKey(categoryDO.getId()) == null) {
            this.insert(categoryDO);
        } else {
            Integer old = categoryDOMapper.selectByPrimaryKey(categoryDO.getId()).getSort();
            Integer now = categoryDO.getSort();
            System.out.println(categoryDO.getParent());
            if (now > old) {
                categoryDOMapper.addCategorySort(old + 1, now, -1, categoryDO.getParent());
            } else if (now < old) {
                categoryDOMapper.addCategorySort(now, old - 1, 1, categoryDO.getParent());
            }
            this.update(categoryDO);
        }
    }

    private void insert(CategoryDO categoryDO) {
        categoryDO.setId(UUIDUtil.getShortUUID());
        categoryDOMapper.insert(categoryDO);
    }

    private void update(CategoryDO categoryDO) {
        categoryDOMapper.updateByPrimaryKey(categoryDO);
    }

    public void delete(String id) {
        categoryDOMapper.deleteByPrimaryKey(id);
    }


    private CategoryModel convertCategoryModelFromCategoryDO(CategoryDO categoryDO) {
        if (categoryDO == null) {
            return null;
        }
        CategoryModel categoryModel = new CategoryModel();
        BeanUtils.copyProperties(categoryDO, categoryModel);
        return categoryModel;
    }
}
